from jili.trade.constants import openclose,status
from jili.pos.module import base
class fund(base):
    def __init__(self,
                 avlcash=0
            , avltransfercash=0
            , frozenfund=0
            , margin=0
            , asset=0
            , pl=0
            , pl_close=0
            , pl_posbydate=0,
                 frozendict={}
                 ):
        super(fund, self).__init__()
        self.avlcash = avlcash
        self.avltransfercash = avltransfercash
        self.frozenfund = frozenfund
        self.margin = margin
        self.asset = asset
        self.pl = pl
        self.pl_close = pl_close
        self.pl_posbydate = pl_posbydate
        self.frozendict=frozendict
    def update_orderdetail(self,oo):
        orderid = oo.orderid
        oc = oo.openclose
        stat = oo.status
        if oc==openclose.open:#处理fund
            if stat == status.working:  # 挂单冻结
                workingamount = oo.workingamount
                tradedamount = oo.tradedamount
                workingqty=oo.workingqty
                if orderid not in self.frozendict.keys():
                    self.frozendict[orderid]={}
                    self.frozendict[orderid]["workingqty"]=oo.workingqty
                    self.frozendict[orderid]["workingamount"]=oo.workingamount
                    self.frozendict[orderid]["cancelamount"] = oo.cancelamount
                    self.frozendict[orderid]["tradedamount"] = oo.tradedamount
                    self.popfrozen(tradedamount)
                    self.frozen(workingamount)
                else:
                    qty0=self.frozendict[orderid]["workingqty"]
                    tamount=self.frozendict[orderid]["tradedamount"]
                    if workingqty<qty0:
                        amount=tradedamount-tamount
                        self.popfrozen(amount)
            elif stat == status.canceled:  # 撤单解冻
                if orderid not in self.frozenfunds.keys():
                    amount = oo.cancelamount
                    self.frozendict[orderid] = self.frozendict[orderid]-amount
                    self.unfrozen(amount)
    def update_tradedetail(self,oo):
        orderid = oo.orderid
        oc = oo.openclose
        stat = oo.status
        tardeid=oo.tradeid
        if oc == openclose.open:  # 处理fund  cash_>pos
            tradedamount = oo.tradedamount
            if orderid not in self.frozendict.keys():
                self.frozendict[orderid] = {}
                self.frozendict[orderid][tardeid] = tradedamount
                self.popfrozen(tradedamount)
            else:
                if tardeid not in self.frozendict[orderid].keys():
                    self.frozendict[orderid][tardeid] = tradedamount
                    self.popfrozen(tradedamount)
        else:
            tradedamount = oo.tradedamount
            if orderid not in self.frozendict.keys():
                self.frozendict[orderid] = {}
                self.frozendict[orderid][tardeid] = tradedamount
                self.addcash(tradedamount)
            else:
                if tardeid not in self.frozendict[orderid].keys():
                    self.frozendict[orderid][tardeid] = tradedamount
                    self.addcash(tradedamount)
    def frozen(self,amount):
        self.avlcash=self.avlcash-amount
        self.frozenfund = self.frozenfund+ amount
    def unfrozen(self,amount):
        if self.frozenfund >=amount:
            self.avlcash = self.avlcash + amount
            self.frozenfund = self.frozenfund - amount
    def popfrozen(self,amount):
        if self.frozenfun >=amount:
            self.frozenfund = self.frozenfund - amount
    def addcash(self,amount):
        self.avlcash=self.avlcash+amount